package com.itboye.sunsun.me.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.homesensen.alert.ByAlert;
import com.itboye.luntan.web.LuntanWebView;
import com.itboye.sunsun.adapter.CollectWordsAdapter;
import com.itboye.sunsun.app.swipemenulistview.SwipeMenu;
import com.itboye.sunsun.app.swipemenulistview.SwipeMenuCreator;
import com.itboye.sunsun.app.swipemenulistview.SwipeMenuItem;
import com.itboye.sunsun.app.swipemenulistview.SwipeMenuListView;
import com.itboye.sunsun.app.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.CollectWordsBean.CollectWordsInnerBean;
import com.itboye.sunsun.beans.WoDeHuiTieBean;
import com.itboye.sunsun.beans.WoDeHuiTieBean.WoDeHuiTieItemBean;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.custome.XListView.IXListViewListener;
import com.itboye.sunsun.luntan.TieZiDetailActivity;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;
import com.itboye.sunsunhome.R;

public class MyShouCangActivity extends BaseActivity {
	private SwipeMenuListView xlistview;
	private int pageNum = 1;

	private BaseAdapter adapter;
	List<WoDeHuiTieItemBean> dataList = new ArrayList<>();

	private TextView add_shap_title_tv;
	private ImageView close_icon;
	private CollectWordsAdapter mAdapter;
	private List<CollectWordsInnerBean> list;
	private List<String> favoriteList;
	private String favourStr = "";
	private int page_size = 10;
	String favorite_value;

	String tid;
	private RelativeLayout gonerela;

	@Override
	@SuppressLint("InlinedApi")
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_myshoucang);
		add_shap_title_tv.setText("我的收藏");
		setStatusBarColor(R.color.home_blue);
		close_icon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		
		xlistview.setPullRefreshEnable(true);
		xlistview.setPullLoadEnable(true);
		xlistview.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				pullUpTwo();
			}

			@Override
			public void onLoadMore() {
				pullUp();
			}

		});
		xlistview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(App.ctx,TieZiDetailActivity.class);
				intent.putExtra("tid", dataList.get(position-1).getTid());
				intent.putExtra("fid", dataList.get(position-1).getFid());
				intent.putExtra("author_id", dataList.get(position-1).getAuthorId());
				startActivity(intent);
			}
		});
		SwipeMenuCreator creator = new SwipeMenuCreator() {

			@Override
			public void create(SwipeMenu menu) {
				// TODO Auto-generated method stub
				SwipeMenuItem deleteItem = new SwipeMenuItem(App.ctx);
				// set item background
				deleteItem.setBackground(new ColorDrawable(getResources()
						.getColor(R.color.home_blue)));
				deleteItem.setTitle("删除");
				deleteItem.setTitleColor(Color.WHITE);
				// // set item width
				deleteItem.setWidth(dp2px(90));
				deleteItem.setTitleSize(18);
				menu.addMenuItem(deleteItem);
			}
		};

		xlistview.setMenuCreator(creator);
		xlistview.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public void onMenuItemClick(int position, SwipeMenu menu, int index) {

				switch (index) {
				case 0:
					tid=dataList.get(position).getTid();
					pullDelete(position,tid);
					break;
				case 1:
					// delete
					// delete(item);
					// mAppList.remove(position);
					// mAdapter.notifyDataSetChanged();
					break;
				}
			}
		});
		pullUp();
		adapter = new CollectWordsAdapter(dataList);
		xlistview.setAdapter(adapter);
	}

	private int dp2px(int dp) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
				getResources().getDisplayMetrics());
	}

	private void pullUp() {
		MyJsonRequest.Builder<WoDeHuiTieBean> builder = new MyJsonRequest.Builder<WoDeHuiTieBean>()
				.param("uid",
						(String) SPUtils.get(App.ctx, null, SPContants.USER_ID,
								"")).apiVer("100")
				.typeKey("BY_ForumThread_getFavorites")
				.param("page_no", "" + pageNum)
				.param("page_size", page_size + "")
				.requestListener(new XRequestListener<WoDeHuiTieBean>() {

					@Override
					public void onResponse(WoDeHuiTieBean bean) {
						// onPullUpFinish(adapter, xlistview, bean);
						pageNum++;
						xlistview.stopLoadMore();
						if (bean.getCount()>0) {
							dataList.addAll(bean.getList());
							adapter.notifyDataSetChanged();
							gonerela.setVisibility(View.GONE);
						} else if (bean.getList()==null) {
							gonerela.setVisibility(View.VISIBLE);
							xlistview.setVisibility(View.GONE);
						}
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						xlistview.stopLoadMore();
						// onPullUpError(adapter, xlistview, exception, code,
						// msg);
					}
				});

		MyJsonRequest<WoDeHuiTieBean> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}
	
	private void pullUpTwo() {
		MyJsonRequest.Builder<WoDeHuiTieBean> builder = new MyJsonRequest.Builder<WoDeHuiTieBean>()
				.param("uid",
						(String) SPUtils.get(App.ctx, null, SPContants.USER_ID,
								"")).apiVer("100")
				.typeKey("BY_ForumThread_getFavorites")
				.param("page_no", "" + pageNum)
				.param("page_size", page_size + "")
				.requestListener(new XRequestListener<WoDeHuiTieBean>() {

					@Override
					public void onResponse(WoDeHuiTieBean bean) {
						// onPullUpFinish(adapter, xlistview, bean);
						pageNum++;
						xlistview.stopLoadMore();
						if (bean.getCount()>0) {
							dataList.addAll(bean.getList());
							adapter.notifyDataSetChanged();
							gonerela.setVisibility(View.GONE);
						} else if (bean.getCount() < 0) {
							gonerela.setVisibility(View.VISIBLE);
						}
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						xlistview.stopLoadMore();
						// onPullUpError(adapter, xlistview, exception, code,
						// msg);
					}
				});

		MyJsonRequest<WoDeHuiTieBean> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}
	
	private void pullDelete(final int position,final String tid) {
		MyJsonRequest.Builder<String> builder = new MyJsonRequest.Builder<String>()
				.param("uid",
						(String) SPUtils.get(App.ctx, null, SPContants.USER_ID,
								"")).apiVer("100")
				.typeKey("BY_ForumThread_favorites")
				.param("tid",tid)
				.param("favorite_value", "0")
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String bean) {
						ByAlert.alert(bean+"");
						Iterator<WoDeHuiTieItemBean> iterator=dataList.iterator();
						while (iterator.hasNext()) {
							WoDeHuiTieItemBean bean2=iterator.next();
							if(bean2.getTid().equals(tid)){
								iterator.remove();
								break;
							}
						}
						adapter.notifyDataSetChanged();
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						xlistview.stopLoadMore();
						// onPullUpError(adapter, xlistview, exception, code,
						// msg);
					}
				});

		MyJsonRequest<String> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

}
